package kr.pe.johnkim.app.mobile.rest.controller;

import kr.pe.johnkim.app.domain.MyHistoryMember;
import kr.pe.johnkim.app.mobile.rest.service.MyHistoryMemberService;
import kr.pe.johnkim.app.mobile.util.MyHistoryCommon;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * My History Member Controller 크래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 1. 26
 */
@Controller
@Path("/myHistoryMemberController")
public class MyHistoryMemberController {
    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(MyHistoryMemberController.class);

    @Autowired
    private MyHistoryMemberService myHistoryMemberService = null;
    @Autowired
    private MyHistoryCommon myHistoryCommon = null;

    /**
     * 사용자 id 중복 검사를 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
//    @RequestMapping(value = "/checkDuplication", method = RequestMethod.POST)
    @POST
    @Path("/checkDuplication")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryMember checkDuplication(InputStream is) throws Exception {
        logger.info("checkDuplication");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("myHistoryMember");

        MyHistoryMember parameterMember = new MyHistoryMember();

        parameterMember.setActivateYn(jsonMember2.getString("activateYn"));
        parameterMember.setId(jsonMember2.getString("id"));

        logger.info("id : {}", parameterMember.getId());

        return myHistoryMemberService.checkDuplication(parameterMember);
    }

    /**
     * 사용자를 등록 한다.
     *
     * 1. 사용자를 등록 한다.
     * 2. 미리 정의된 Group 목록을 등록 한다.
     * 3. Group id 를 가져 와서 Category 목록을 등록 한다.
     * 4. Category id 를 가져 와서 Item 목록을 등록 한다.
     * 5. 모두 한개의 Transaction 으로 묶어서 처리 한다.
     * 
     * @param is
     * @return
     * @throws Exception
     */
//    @RequestMapping(value = "/registerMember", method = RequestMethod.POST)
    @POST
    @Path("/registerMember")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryMember registerMember(InputStream is) throws Exception {
        logger.info("registerMember");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("myHistoryMember");

        MyHistoryMember parameterMember = new MyHistoryMember();

        parameterMember.setId(jsonMember2.getString("id"));
        parameterMember.setPassword(myHistoryCommon.getEncodedPassword(jsonMember2.getString("password")));
        parameterMember.setEmail(jsonMember2.getString("email"));
        parameterMember.setOpenEmailYn(jsonMember2.getString("openEmailYn"));
        parameterMember.setMobilePhoneNumber(jsonMember2.getString("mobilePhoneNumber"));
        parameterMember.setOpenMobilePhoneYn(jsonMember2.getString("openMobilePhoneYn"));
        parameterMember.setCountry(jsonMember2.getString("country"));
        parameterMember.setOpenCountryYn(jsonMember2.getString("openCountryYn"));
        parameterMember.setHomePage(jsonMember2.getString("homePage"));
        parameterMember.setOpenHomePageYn(jsonMember2.getString("openHomePageYn"));
        parameterMember.setNickName(jsonMember2.getString("nickName"));
        parameterMember.setOpenNickNameYn(jsonMember2.getString("openNickNameYn"));
        parameterMember.setProfileImageUrl(jsonMember2.getString("profileImageUrl"));
        parameterMember.setOpenProfileImageYn(jsonMember2.getString("openProfileImageYn"));
        parameterMember.setFacebookId(jsonMember2.getString("facebookId"));
        parameterMember.setFacebookPassword(jsonMember2.getString("facebookPassword"));
        parameterMember.setOpenFacebookYn(jsonMember2.getString("openFacebookYn"));
        parameterMember.setTwitterId(jsonMember2.getString("twitterId"));
        parameterMember.setTwitterPassword(jsonMember2.getString("twitterPassword"));
        parameterMember.setOpenTwitterYn(jsonMember2.getString("openTwitterYn"));
        parameterMember.setBirthday(jsonMember2.getString("birthday"));
        parameterMember.setSex(jsonMember2.getString("sex"));
        parameterMember.setMileage(jsonMember2.getString("mileage"));
        parameterMember.setLevel(jsonMember2.getString("level"));
        parameterMember.setActivateYn(jsonMember2.getString("activateYn"));
        parameterMember.setModifyDate(jsonMember2.getString("modifyDate"));
        parameterMember.setRegisterDate(jsonMember2.getString("registerDate"));

        logger.info("id                 : {}", parameterMember.getId());
        logger.info("password(before)   : {}", jsonMember2.getString("password"));
        logger.info("password(after)    : {}", parameterMember.getPassword());
        logger.info("email              : {}", parameterMember.getEmail());
        logger.info("openEmilYn         : {}", parameterMember.getOpenEmailYn());
        logger.info("mobilePhoneNumber  : {}", parameterMember.getMobilePhoneNumber());
        logger.info("openMobilePhoneYn  : {}", parameterMember.getOpenMobilePhoneYn());
        logger.info("country            : {}", parameterMember.getCountry());
        logger.info("openCountryYn      : {}", parameterMember.getOpenCountryYn());
        logger.info("homePage           : {}", parameterMember.getHomePage());
        logger.info("openHomePageYn     : {}", parameterMember.getOpenHomePageYn());
        logger.info("nickName           : {}", parameterMember.getNickName());
        logger.info("openNickNameYn     : {}", parameterMember.getOpenNickNameYn());
        logger.info("profileImageUrl    : {}", parameterMember.getProfileImageUrl());
        logger.info("openProfileImageYn : {}", parameterMember.getOpenProfileImageYn());
        logger.info("facebookId         : {}", parameterMember.getFacebookId());
        logger.info("facebookPassword   : {}", parameterMember.getFacebookPassword());
        logger.info("openFacebookYn     : {}", parameterMember.getOpenFacebookYn());
        logger.info("twitterId          : {}", parameterMember.getTwitterId());
        logger.info("twitterPassword    : {}", parameterMember.getTwitterPassword());
        logger.info("openTwitterYn      : {}", parameterMember.getOpenTwitterYn());
        logger.info("birthday           : {}", parameterMember.getBirthday());
        logger.info("sex                : {}", parameterMember.getSex());
        logger.info("mileage            : {}", parameterMember.getMileage());
        logger.info("level              : {}", parameterMember.getLevel());
        logger.info("activateYn         : {}", parameterMember.getActivateYn());
        logger.info("modifyDate         : {}", parameterMember.getModifyDate());
        logger.info("registerDate       : {}", parameterMember.getRegisterDate());

        MyHistoryMember resultMember = new MyHistoryMember();

        if(myHistoryMemberService.registerMember(parameterMember)) {
            resultMember.setResult("SUCCESS");
        } else {
            resultMember.setResult("FAIL");
        }

        return resultMember;
    }

    /**
     * 사용자 로그인을 한다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/login")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryMember login(InputStream is) throws IOException, JSONException {
        logger.info("login");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("myHistoryMember");

        MyHistoryMember parameterMember = new MyHistoryMember();

        parameterMember.setId(jsonMember2.getString("id"));
        parameterMember.setPassword(myHistoryCommon.getEncodedPassword(jsonMember2.getString("password")));

        logger.info("id       : {}", parameterMember.getId());
        logger.info("password : {}", parameterMember.getPassword());

//        return myHistoryMemberDao.login(parameterMember);

        return myHistoryMemberService.login(parameterMember);
    }

    /**
     * 사용자의 마일리지를 가져 온다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/selectMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryMember selectMileage(InputStream is) throws IOException, JSONException {
        logger.info("selectMileage");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("myHistoryMember");

        MyHistoryMember parameterMember = new MyHistoryMember();

        parameterMember.setId(jsonMember2.getString("id"));

        logger.info("id : {}", parameterMember.getId());

        return myHistoryMemberService.selectMileage(parameterMember);
    }

    /**
     * 사용자의 마일리지를 업데이트(추가) 한다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/updateMileage")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryMember updateMileage(InputStream is) throws IOException, JSONException {
        logger.info("updateMileage");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("myHistoryMember");

        MyHistoryMember parameterMember = new MyHistoryMember();

        parameterMember.setId(jsonMember2.getString("id"));
        parameterMember.setMileage(jsonMember2.getString("mileage"));
        parameterMember.setModifyDate(jsonMember2.getString("modifyDate"));

        logger.info("id      : {}", parameterMember.getId());
        logger.info("mileage : {}", parameterMember.getMileage());
        logger.info("modifyDate   : {}", parameterMember.getModifyDate());

        MyHistoryMember resultMember = new MyHistoryMember();

        if(myHistoryMemberService.updateMileage(parameterMember)) {
            resultMember.setResult("SUCCESS");
        } else {
            resultMember.setResult("FAIL");
        }

        return resultMember;
    }

    /**
     * 사용자 비밀번호를 변경 한다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/updatePassword")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryMember updatePassword(InputStream is) throws IOException, JSONException {
        logger.info("updatePassword");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("myHistoryMember");

        MyHistoryMember parameterMember = new MyHistoryMember();

        parameterMember.setId(jsonMember2.getString("id"));
        parameterMember.setPassword(myHistoryCommon.getEncodedPassword(jsonMember2.getString("password")));
        parameterMember.setModifyDate(jsonMember2.getString("modifyDate"));

        logger.info("id         : {}", parameterMember.getId());
        logger.info("password   : {}", jsonMember2.getString("password"));
        logger.info("modifyDate : {}", parameterMember.getModifyDate());

        MyHistoryMember resultMember = new MyHistoryMember();

        if(myHistoryMemberService.updatePassword(parameterMember)) {
            resultMember.setResult("SUCCESS");
        } else {
            resultMember.setResult("FAIL");
        }

        return resultMember;
    }

    /**
     * 사용자 이메일을 업데이트 한다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/updateEmail")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryMember updateEmail(InputStream is) throws IOException, JSONException {
        logger.info("updateEmail");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonMember = new JSONObject(line);
        JSONObject jsonMember2 = jsonMember.getJSONObject("myHistoryMember");

        MyHistoryMember parameterMember = new MyHistoryMember();

        parameterMember.setId(jsonMember2.getString("id"));
        parameterMember.setEmail(jsonMember2.getString("email"));
        parameterMember.setModifyDate(jsonMember2.getString("modifyDate"));

        logger.info("id         : {}", parameterMember.getId());
        logger.info("email      : {}", parameterMember.getEmail());
        logger.info("modifyDate : {}", parameterMember.getModifyDate());

        MyHistoryMember resultMember = new MyHistoryMember();

        if(myHistoryMemberService.updateEmail(parameterMember)) {
            resultMember.setResult("SUCCESS");
        } else {
            resultMember.setResult("FAIL");
        }

        return resultMember;
    }

    /**
     * Spring Setter Injection.
     */
//    private MyHistoryMemberService myHistoryMemberService;
//    private MyHistoryCommon myHistoryCommon;
//
//    public void setMyHistoryMemberService(MyHistoryMemberService myHistoryMemberService) {
//        this.myHistoryMemberService = myHistoryMemberService;
//    }
//
//    public void setMyHistoryCommon(MyHistoryCommon myHistoryCommon) {
//        this.myHistoryCommon = myHistoryCommon;
//    }
}
